package com.yr.ryjk.score.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yr.ryjk.common.ErrorType;
import com.yr.ryjk.common.Pagination;
import com.yr.ryjk.doctor.dao.DoctorMapper;
import com.yr.ryjk.doctor.entity.Doctor;
import com.yr.ryjk.jpush.JpushMessage;
import com.yr.ryjk.jpush.JpushSendService;
import com.yr.ryjk.redis.JedisUtil;
import com.yr.ryjk.score.dao.ScoreMapper;
import com.yr.ryjk.score.entity.Score;
import com.yr.ryjk.score.entity.ScoreVO;
import com.yr.ryjk.score.entity.ScoreVO2;
import com.yr.ryjk.user.dao.UserMapper;
import com.yr.ryjk.user.entity.User;
import com.yr.ryjk.util.IDGenerator;
import com.yr.ryjk.util.JSONUtil;

@Service
public class ScoreService implements IScoreService{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private ScoreMapper scoreMapper;
	@Autowired
	private DoctorMapper doctorMapper;
	
	@Override
	public Map<String, Object> addScore(String consultId, String commentContent, int scoreNum,String token) {
			Score score=new Score();
	        score.setScoreId(IDGenerator.gen32UUID());
	        
	        //1.验证令牌
	    	String phone=JedisUtil.getPhoneByToken(token);
	    	User user = null;
	    	if(phone!=null){
				user=userMapper.selectUserByPhone(phone);
				score.setUserId(user.getId());
			}else {
				return JSONUtil.getJSON(ErrorType.TOKEN_ERROR);
			}
	    	score.setConsultId(consultId);
	    	
	    	//2.验证是否重复评分
	    	int c=scoreMapper.selectCommentByConsultId(consultId);
	    	if(c==0){
	    		score.setCommentContent(commentContent);
	    	}else {
	    		return JSONUtil.getJSON(ErrorType.SCORE_REPEAT_ERROR);
			}
    	    score.setScoreNum(scoreNum);
    	    score.setCommentTime(new Date());
    	    scoreMapper.insert(score);
    	    
    	    //3.极光推送
    	    final Doctor doctor=doctorMapper.selectByPrimaryKey(consultId);
    	    final int scoreNum2=scoreNum;
    	    final String commentContent2=commentContent;
    	    new Thread(new Runnable() {
				@Override
				public void run() {
					JpushMessage msg = new JpushMessage();
					msg.setTitle("睿翼健康");
					msg.setContent("收到病人评价，分数:"+scoreNum2+";内容:"+commentContent2);
					
					String jpDevice  = JedisUtil.getJPushKeyByPhone(doctor.getPhone());
					JpushSendService.sendAndroidNotice(msg, new HashMap<String, String>(), new String[]{jpDevice});//发送健康币
				}
			}).start();
    	    return JSONUtil.getJSON(ErrorType.SUCCESS);
	}
	
	@Override
	public Map<String, Object> getListOfPage(int pageNo, int pageSize,
			String token){
		String phone=JedisUtil.getPhoneByToken(token);
		User user=userMapper.selectUserByPhone(phone);	
		List<ScoreVO> list=scoreMapper.selectByUserId((pageNo-1)*pageSize,pageSize,user.getId());		
		
		int count=scoreMapper.selectCount(user.getId());
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", new Pagination(pageSize,pageNo,count));
		
		return map;
	}
	
	@Override
	public Map<String, Object> getListOfScores(int pageNo, int pageSize,
			String token) 
	{
		String phone=JedisUtil.getPhoneByToken(token);
		Doctor doctor=doctorMapper.selectByPhone(phone);
		List<ScoreVO2> list=scoreMapper.selectByDocId((pageNo-1)*pageSize,pageSize,doctor.getId());		
		
		int count=scoreMapper.selectCountOfScores(doctor.getId());
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		map.put("pagination", new Pagination(pageSize,pageNo,count));
		
		return map;
	}
}